<template>
  <div>
    <div class="permissManaTools">
      <el-input placeholder="请输入角色英文名"
                v-model="role.name"
                @keydown.enter.native="doAddRole">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input v-model="role.nameZh"
                placeholder="请输入角色中文名"
                @keydown.enter.native="doAddRole"></el-input>
      <el-button type="primary"
                 icon="el-icon-plus"
                 @click="doAddRole">添加角色</el-button>
    </div>
    <div class="permissManaMain">
      <!--Collapse 折叠面板-->
      <el-collapse accordion @change="change" v-model="activeName">
        <el-collapse-item v-for="(item, index) in roles"
                          :title="item.nameZh"
                          :key="index"
                          :name="item.id">
          <!--Card 卡片-->
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <!--删除角色按钮-->
              <el-button style="float: right; padding: 3px 0; color: red;"
                         type="text"
                         @click="doDeleteRole(item)"
                         icon="el-icon-delete"></el-button>
            </div>
            <div>
              <!--Tree 树形控件-->
              <el-tree show-checkbox
                       :data="allMenus"
                       :default-checked-keys="selectedMenus"
                       :key="index"
                       node-key="id"
                       ref="tree"
                       :props="defaultProps"></el-tree>
              <div class="treeButton">
                <el-button @click="cancelUpdate">取消修改</el-button>
                <el-button type="primary" @click="doUpdate(item.id, index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
  export default {
    name: "PermissMana",
    data() {
      return {
        role: {
          name: '',
          nameZh: ''
        },
        roles: [],
        allMenus: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        selectedMenus: [],
        activeName: -1
      }
    },
    methods: {
      initRoles() {
        this.getRequest('/system/basic/permission/').then(resp => {
          if (resp) {
            this.roles = resp
          }
        })
      },
      initAllMenus() {
        this.getRequest('/system/basic/permission/menus').then(resp => {
          if (resp) {
            this.allMenus = resp
          }
        })
      },
      initSelectedMenus(rid) {
        this.getRequest('/system/basic/permission/mid/' + rid).then(resp => {
          if (resp) {
            this.selectedMenus = resp
          }
        })
      },
      change(rid) {
        if (rid) {
          this.initAllMenus()
          this.initSelectedMenus(rid)
        }
      },
      doUpdate(rid, index) {
        let tree = this.$refs.tree[index]
        // getCheckedKeys: elementUI的tree自带的函数
        // (leafOnly) 接收一个 boolean 类型的参数，若为 true 则仅返回被选中的叶子节点的 keys，默认值为 false
        let selectedKeys = tree.getCheckedKeys(true)
        let url = '/system/basic/permission/?rid=' + rid
        selectedKeys.forEach(key => {
          url += '&mids=' + key
        })
        this.putRequest(url).then(resp => {
          if (resp) {
            this.activeName = -1
          }
        })
      },
      cancelUpdate() {
        this.activeName = -1
      },
      doAddRole() {
        if (this.role.name && this.role.nameZh) {
          this.postRequest('/system/basic/permission/role', this.role).then(resp => {
            if (resp) {
              this.initRoles()
              this.role.name = ''
              this.role.nameZh = ''
            }
          })
        } else {
          this.$message.error('所有字段不能为空')
        }
      },
      doDeleteRole(role) {
        //  MessageBox 弹框
        this.$confirm('此操作将永久删除['+ role.nameZh +']职位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/basic/permission/role/' + role.id).then(resp => {
            if (resp) {
              this.initRoles()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    mounted() {
      this.initRoles()
    }
  }
</script>

<style>

  .permissManaTools {
    display: flex;
    justify-content: flex-start;
  }

  .permissManaTools .el-input {
    width: 300px;
    margin-right: 6px;
  }

  .permissManaMain {
    margin-top: 10px;
    width: 700px;
  }

  .treeButton {
    display: flex;
    justify-content: flex-end;
  }

</style>